package com.onesatoshi.mm.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserDao {

    private DBHelp dbHelp;
    private SQLiteDatabase db;

    public UserDao(Context context) {
        dbHelp = new DBHelp(context);
    }

    public void save(User user) {
        db = dbHelp.getWritableDatabase();
        String sql = "insert into t_user(username, phone, status, nickname, wxid) values(?,?,?,?,?)";
        db.execSQL(sql, new String[]{user.getUsername(), user.getPhone(),
                user.getStatus(), user.getNickname(), user.getWxid()});
        db.close();
    }

    public void update(User user) {
        db = dbHelp.getWritableDatabase();
        String sql = "update t_user set username = ?,phone = ?, status=?, nickname = ?, wxid = ? where _id = ?";
        db.execSQL(sql, new Object[]{user.getUsername(), user.getPhone(), user.getStatus(),
                user.getNickname(), user.getWxid(), user.getId()});
        db.close();
    }

    public void del(int id) {
        db = dbHelp.getWritableDatabase();
        String sql = "delete from t_user where _id = ?";
        db.execSQL(sql, new Object[]{id});
        db.close();
    }

    public List<User> findAll() {
        List<User> list = new ArrayList<User>();

        db = dbHelp.getReadableDatabase();
        String sql = "select _id, username, phone, status, nickname, wxid from t_user";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            User user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            user.setStatus(cursor.getString(cursor.getColumnIndex("status")));
            user.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            user.setWxid(cursor.getString(cursor.getColumnIndex("wxid")));
            list.add(user);
        }
        db.close();
        return list;
    }

    public List<User> findAllToDelete() {
        List<User> list = new ArrayList<User>();

        db = dbHelp.getReadableDatabase();
        String sql = "select _id, username, phone, status, nickname, wxid from t_user where status != '0'";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            User user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            user.setStatus(cursor.getString(cursor.getColumnIndex("status")));
            user.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            user.setWxid(cursor.getString(cursor.getColumnIndex("wxid")));
            list.add(user);
        }
        db.close();
        return list;
    }


    public User findByID(int id) {
        User user = null;
        db = dbHelp.getReadableDatabase();
        String sql = "select _id,username,phone,status, nickname, wxid from t_user where _id = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(id)});

        while (cursor.moveToNext()) {
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            user.setStatus(cursor.getString(cursor.getColumnIndex("status")));
            user.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            user.setWxid(cursor.getString(cursor.getColumnIndex("wxid")));
        }

        db.close();
        return user;
    }


    public User findByPhone(String phone) {
        User user = null;
        db = dbHelp.getReadableDatabase();
        String sql = "select _id, username, phone, status, nickname, wxid from t_user where phone = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{phone});

        while (cursor.moveToNext()) {
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            user.setStatus(cursor.getString(cursor.getColumnIndex("status")));
            user.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            user.setWxid(cursor.getString(cursor.getColumnIndex("wxid")));
        }

        db.close();
        return user;
    }

    /**
     * 查找可以添加的第一个号码
     *
     * @return
     */
    public User findTop2Add() {
        User user = null;
        db = dbHelp.getReadableDatabase();
        String sql = "select _id, username, phone, status, nickname, wxid from t_user where status = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{"0"});

        while (cursor.moveToNext()) {
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            user.setStatus(cursor.getString(cursor.getColumnIndex("status")));
            user.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            user.setWxid(cursor.getString(cursor.getColumnIndex("wxid")));
        }

        db.close();
        return user;
    }

    /**
     * 统计未处理的条数
     *
     * @return
     */
    public int countUndoNum() {
        db = dbHelp.getReadableDatabase();
        String sql = "select count(1) c from t_user where status = ?";

        Cursor cursor = db.rawQuery(sql, new String[]{"0"});

        int cnt = -1;
        if (cursor.moveToNext()) {
            cnt = cursor.getInt(0);
        }
        db.close();

        return cnt;
    }

}
